#-*-coding:utf-8-*-
'''
Created on 2011-1-16

@author: zhaoyh
'''
import web
from admin.config import view
from admin.models.auth import Group
from core.meta import PageList,DBSession
from sqlalchemy.exc import IntegrityError
compo_name='用户组'
titles={'group_code':'用户组代码','group_name':'用户组名称'}    
class list():
    '''
         请求页面时使用get，提交数据时使用post
    '''
    def GET(self,name):
        page=web.input(numPerPage=20,pageNum=1,keywords='',orderField='group_code',col='')
        if page.col=='' or page.col==None:
            col=['group_code','group_name']
        else:
            col=[page.col]
        list=PageList(Group,page,page.orderField,page.numPerPage,page.pageNum,col,page.keywords)      
        return view.lookup_list_page(groups=list.getlist(),page=list.showPage(),title=titles)
class tree():
    def GET(self):
        return view.group_add()
    def POST(self):
        u=web.input()
        new_Group=Group(u.group_code,u.group_name)
        try:
            T=DBSession.query(Group).filter(Group.group_code==u['group_code']).first()
            if T: 
                return '{"statusCode":"300", "message":"%s代码重复"}' %compo_name
            else:
                DBSession.add(new_Group)
                DBSession.commit()
                DBSession.flush()
                return '{"statusCode":"200","message":"添加成功","navTabId":"group_list","dialogid":"group_add", "callbackType":"forward","dialogUrl":"/admin/group/add"}'
        except IntegrityError:
            DBSession.rollback()
            return '{"statusCode":"300", "message":"用户组代码重复"}'
        except Exception,e:
            print Exception,e
            DBSession.rollback()
            return  '{"statusCode":"300", "message":"保存错误"}'     
            
class json():
    def GET(self,group_id):
        edit_Group=DBSession.query(Group).filter(Group.group_id==group_id).first()
        return view.group_edit(group=edit_Group)
    def POST(self,name):
        u=web.input()
        try:
            edit_group=DBSession.query(Group).filter(Group.group_id==u.group_id).one()
            edit_group.group_code=u.group_code
            edit_group.group_name=u.group_name
            DBSession.commit()
            DBSession.flush()
            return '{"statusCode":"200","message":"保存成功","navTabId":"group_list","dialogid":"group_edit", "callbackType":"forward","dialogUrl":"/admin/group/edit/"}'
        except Exception,e:
            DBSession.rollback()
            print Exception,e
            return  '{"statusCode":"300", "message":"保存错误"}' 
class xml():
    def POST(self,group_id):
        try:
            edit_Group=DBSession.query(Group).filter(Group.group_id==group_id).one()
            DBSession.delete(edit_Group)
            DBSession.commit()
            DBSession.flush()
            return '{"statusCode":"200","message":"删除成功","navTabId":"group_list"}'
        except Exception,e:
            DBSession.rollback()
            print Exception,e
            return  '{"statusCode":"300", "message":"删除错误"}' 
